﻿#include "mainwindows.h"
#include <chmslog.h>

#include <QTextCodec>
#include <QFile>
#include <QTextStream>
#include <QSettings>


MainWindows::MainWindows(QWidget *parent)
    : QWidget{parent}
{
    /*
     * 加载程序基础设置
     * */
    pAppBase = new AppBase();

    /*
     * 初始化配置文件
     * */
    pAppBase->funVoidConfigInitializes();

    /*
     * 读取配置文件
     * @varItemSelect : 左侧列表最后一次关闭程序时所在位置。
     * @debugRun : 是否开启 debug 调试记录日志
     * */
    AppBase::varItemSelect = pAppBase->funQStrReadConfig("LeftMenu","leftList").toInt();
    AppBase::debugRun = pAppBase->funQStrReadConfig("LogDebug","debugRun").toInt();

    /*
     * 以下为界面布局代码
     * */
    setMinimumHeight(650);

    // 主界面 水平布局
    pObjQHBoxLayout = new QHBoxLayout(this);

    pObjQVBoxLayoutLeft = new QVBoxLayout;  // 左侧承载 QListWidget 的垂直布局
    pObjQScrollArea = new QScrollArea(this);  // 右侧界面: pObjQScrollArea

    pObjQListWidget = new QListWidget(this);  // 左侧列表: pObjQListWidget
    pObjQPusButAbout = new QPushButton(this);  // 左侧 关于 按钮

    pObjDFPPP = new DerForPasswdProdPanel;  // 右侧 密码模块窗体
    pObjIPMaskCal = new ClassForIPMaskCalculate; // 右侧呈现 掩码模块儿
    pObjHostDiscover = new ClassForHostDiscover; // 右侧 网络主机发现模块

    pFormAbout = new FormAbout;  // 关于窗口

    pObjQHBoxLayout->addLayout(pObjQVBoxLayoutLeft);  // 主界面 加载左侧的垂直布局
    pObjQHBoxLayout->addWidget(pObjQScrollArea);  // 主界面 加载 右侧 QScrollArea

    pObjQVBoxLayoutLeft->addWidget(pObjQListWidget);  // 左侧布局加载 QListWidget
    pObjQVBoxLayoutLeft->addWidget(pObjQPusButAbout);  // 左侧布局加载 关于 按钮

    pObjQScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    pObjQScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);

    pObjQScrollArea->setWidgetResizable(true);
    pObjQListWidget->setFrameShape(QFrame::NoFrame);
    pObjQScrollArea->setFrameShape(QFrame::NoFrame);

    /****   以上为界面布局   ****/

    /*
     * 读取 qss 皮肤文件
     * */
    QFile objFileSkin(":/res/Style/mainskin.qss");
    QString varQStrSkin;
    if(objFileSkin.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream qssInput(&objFileSkin);
        while(!qssInput.atEnd())
        {
            varQStrSkin += qssInput.readLine();
        }
    }
    objFileSkin.close();
    this->setStyleSheet(varQStrSkin);

    // 界面控件初始化
    MainWindows::funVoidWindowsIniti(this,AppBase::varItemSelect);

    connect(pObjQPusButAbout,&QPushButton::clicked,this,&MainWindows::on_pObjQPusButAbout_clicked);
    connect(pObjQListWidget,&QListWidget::itemClicked,this,&MainWindows::on_pObjQListWidget_itemClicked);

    QByteArray logTemp1 = "程序开始运行->";
    ChMSLog::funVoidConDisInfo_Chinese(logTemp1);
    ChMSLog::funVoidWriteLog(logTemp1);

}

MainWindows::~MainWindows()
{

    pAppBase->funVoidWriteConfig("LeftMenu","leftList",QString::number(this->varIntLeftList));
    //pAppBase->funVoidWriteConfig("LogDebug","debugRun","0");


    delete pObjHostDiscover;  // 网络主机发现模块
    pObjHostDiscover = nullptr;

    delete pObjIPMaskCal;  // 右侧呈现 掩码模块。
    pObjIPMaskCal = nullptr;


    // 程序基础设置
    delete pAppBase;  // 清空基础对像
    pAppBase = nullptr;

    /*
     * 写日志
     * */
    QByteArray logTemp1 = "程序结束运行。";
    ChMSLog::funVoidConDisInfo_Chinese(logTemp1);
    ChMSLog::funVoidWriteLog(logTemp1);

}

void MainWindows::on_pObjQPusButAbout_clicked()
{

    pFormAbout->show();

}

// 点击左侧列表 在右侧显示对应的功能模块儿
void MainWindows::on_pObjQListWidget_itemClicked()
{
    int varItemTemp = 0;

    varItemTemp = pObjQListWidget->currentRow();

    switch(varItemTemp)
    {
    case 0:
        do{

            pObjQScrollArea->takeWidget();
            pObjQScrollArea->setWidget(pObjDFPPP);
            this->varIntLeftList = 0;
        }while(0);

        break;
    case 1:
        do{
            pObjQScrollArea->takeWidget();
            pObjQScrollArea->setWidget(pObjIPMaskCal);
            this->varIntLeftList = 1;
        }while(0);

        break;
    case 2:
        do{
            pObjQScrollArea->takeWidget();
            pObjQScrollArea->setWidget(pObjHostDiscover);
            this->varIntLeftList = 2;
        }while(0);
    default:
        break;
    }


}

/*
 * 界面初始化函数
 * */
void MainWindows::funVoidWindowsIniti(MainWindows *p, const int &itemSelect) const
{
    /*
     * 设置左侧列表和右侧区域的大小
     * */
    p->pObjQListWidget->setFixedWidth(150);
    p->pObjQScrollArea->setFixedWidth(850);

    /*
     * 中文转码区
     * */
    ChMSTextCodec objTextCode;

    QByteArray varQByArrOfLeftListItem1 = "生产密码";
    QByteArray varQByArrOfLeftListItem2 = "网络掩码";
    QByteArray varQByArrOfLeftListItem3 = "主机探测";
    QByteArray varQByArrOfLeftListItem4 = "关于";

    QString varUnicodeTmp1 = objTextCode.funQStrTextCodec(varQByArrOfLeftListItem1);
    QString varUnicodeTmp2 = objTextCode.funQStrTextCodec(varQByArrOfLeftListItem2);
    QString varUnicodeTmp3 = objTextCode.funQStrTextCodec(varQByArrOfLeftListItem3);
    QString varUnicodeTmp4 = objTextCode.funQStrTextCodec(varQByArrOfLeftListItem4);

    /*
    QString varUnicodeTmp1 = ChMSTc.funQStrTextCodec(varQByArrOfLeftListItem1);
    QString varUnicodeTmp2 = ChMSTc.funQStrTextCodec(varQByArrOfLeftListItem2);
    QString varUnicodeTmp3 = ChMSTc.funQStrTextCodec(varQByArrOfLeftListItem3);
    QString varUnicodeTmp4 = ChMSTc.funQStrTextCodec(varQByArrOfLeftListItem4);
    */

    // 加载左侧列表
    p->varObjQStrLisLefList << varUnicodeTmp1 << varUnicodeTmp2 << varUnicodeTmp3;
    //p->varObjQStrLisLefList << varUnicodeTmp1 << varUnicodeTmp2;
    p->pObjQListWidget->addItems(p->varObjQStrLisLefList);
    // 关于按钮
    p->pObjQPusButAbout->setText(varUnicodeTmp4);

    switch(itemSelect)
    {
    case 0:
        do{
            pObjQScrollArea->setWidget(pObjDFPPP);
            p->varIntLeftList = 0;
        }while(0);
        break;
    case 1:
        do{
            pObjQScrollArea->setWidget(pObjIPMaskCal);
            p->varIntLeftList = 1;
        }while(0);
        break;
    case 2:
        do{
            pObjQScrollArea->setWidget(pObjHostDiscover);
            p->varIntLeftList = 2;
        }while(0);
        break;
    default:
            pObjQScrollArea->setWidget(pObjIPMaskCal);
            p->varIntLeftList = 1;
        break;
    }
}









